package com.xbongbong.paas.help.symbol;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.constant.BusinessConstant;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 地址进行校验
 * 地址
 * @author 吴峰
 * @date 2021/01/07 14:32
 */
public class AddressSymbolHandle extends TextSymbolHandle {

    @Override
    public boolean equals(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        JSONObject jsonObject = source.getData().getJSONObject(fieldAttrEntity.getAttr());
        String sourceProvince = jsonObject.getString(BusinessConstant.PROVINCE);
        String sourceCity = jsonObject.getString(BusinessConstant.CITY);
        String sourceDistrict = jsonObject.getString(BusinessConstant.DISTRICT);
        String sourceAddress = jsonObject.getString(BusinessConstant.ADDRESS);
        JSONObject targetJson = JSON.parseObject(target.toString());
        String targetProvince = targetJson.getString(BusinessConstant.PROVINCE);
        String targetCity = targetJson.getString(BusinessConstant.CITY);
        String targetDistrict = targetJson.getString(BusinessConstant.DISTRICT);
        String targetAddress = targetJson.getString(BusinessConstant.ADDRESS);

        if (Objects.equals(1, fieldAttrEntity.getShowDetailAddress())) {
            return Objects.equals(sourceProvince, targetProvince) && Objects.equals(sourceCity, targetCity) && Objects.equals(sourceDistrict, targetDistrict)
                    && Objects.equals(sourceAddress, targetAddress);
        } else {
            return Objects.equals(sourceProvince, targetProvince) && Objects.equals(sourceCity, targetCity) && Objects.equals(sourceDistrict, targetDistrict);
        }
    }

    @Override
    public boolean noEquals(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        return !equals(target, source, fieldAttrEntity);
    }

    @Override
    public boolean empty(PaasFormDataEntity sourceData, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Object source = sourceData.getData().get(fieldAttrEntity.getAttr());
        if (Objects.isNull(source) || StringUtil.isEmpty(source.toString())) {
            return true;
        } else if (source instanceof Map) {
            JSONObject address = JSON.parseObject(JSON.toJSONString(source));
            if (Objects.isNull(address) || address.isEmpty()) {
                return true;
            }
            String province = address.getString(BusinessConstant.PROVINCE);
            String city = address.getString(BusinessConstant.CITY);
            String district = address.getString(BusinessConstant.DISTRICT);
            String detail = address.getString(BusinessConstant.ADDRESS);
            return StringUtil.isEmpty(province) && StringUtil.isEmpty(city) && StringUtil.isEmpty(district) && StringUtil.isEmpty(detail);
        }
        return false;
    }

    @Override
    public boolean noEmpty(PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        return !empty(source, fieldAttrEntity);
    }
}
